package com.anjubao.parking.municipal.parkinglot.service;

import com.anjubao.parking.municipal.common.dto.ApiResult;
import com.anjubao.parking.municipal.common.global.BusinessCodeEnum;
import com.anjubao.parking.municipal.common.utils.BeanUtils;
import com.anjubao.parking.municipal.common.utils.MunicipalParkingUtils;
import com.anjubao.parking.municipal.common.utils.U;
import com.anjubao.parking.municipal.parkinglot.constant.DeviceOperationStatusEnum;
import com.anjubao.parking.municipal.parkinglot.constant.DeviceStatusEnum;
import com.anjubao.parking.municipal.parkinglot.constant.DeviceTypeEnum;
import com.anjubao.parking.municipal.parkinglot.dto.*;
import com.anjubao.parking.municipal.parkinglot.feign.ParkingLotFeignClient;
import com.anjubao.parking.municipal.parkinglot.form.GuidanceScreenAddForm;
import com.anjubao.parking.municipal.parkinglot.form.GuidanceScreenEditForm;
import com.anjubao.parking.municipal.parkinglot.form.GuidanceScreenQueryForm;
import com.anjubao.parking.municipal.parkinglot.mapper.*;
import com.anjubao.parking.municipal.parkinglot.model.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 
 * ParkingLot服务实现类
 * 
 * @author chenfang
 *
 */
@Service
@Slf4j
public class ParkingLotDeviceGuidanceScreenService extends ServiceImpl<ParkingLotDeviceGuidanceScreenMapper, ParkingLotDeviceGuidanceScreen> {

    @Resource
    private ParkingLotDeviceGuidanceScreenParkingLotMapper parkingLotDeviceGuidanceScreenParkingLotMapper;

    @Resource
    private ParkingLotDeviceGuidanceScreenMapper parkingLotDeviceGuidanceScreenMapper;

    @Autowired
    private ParkingLotDeviceGuidanceScreenParkingLotService parkingLotDeviceGuidanceScreenParkingLotService;

    @Autowired
    private ParkingLotFeignClient parkingLotFeignClient;

    @Resource
    private ParkingLotDeviceStatusMapper parkingLotDeviceStatusMapper;

    /**
     * 新增诱导屏
     * @param form
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public ParkingLotDeviceGuidanceScreen saveGuidanceScreen(GuidanceScreenAddForm form) {
        ParkingLotDeviceGuidanceScreen guidanceScreen=BeanUtils.transform(ParkingLotDeviceGuidanceScreen.class, form);
        List<String> pklotCodes =new ArrayList<>();
        List<ParkingLotDeviceGuidanceScreenParkingLot> parkingLots=new ArrayList<>();
        guidanceScreen.setId(U.id());//新增
        guidanceScreen.setCode(MunicipalParkingUtils.generateCode(guidanceScreen.getId(), BusinessCodeEnum.GUIDANCE_SCREEN.getCode()));
        //组装停车场关联实体list
        if(form.getParkingLotCodes()!=null){
            pklotCodes=form.getParkingLotCodes();

            for(int i=0;i<pklotCodes.size();i++){
                parkingLots.add(new ParkingLotDeviceGuidanceScreenParkingLot().setSort(i+1)
                        .setParkingLotId(MunicipalParkingUtils.getIdByCode(pklotCodes.get(i)))
                        .setParkingLotDeviceGuidanceScreenId(MunicipalParkingUtils.getIdByCode(guidanceScreen.getCode())));
            }
        }
        save(guidanceScreen);
        parkingLotDeviceGuidanceScreenParkingLotService.saveBatch(parkingLots,parkingLots.size());
        ParkingLotDeviceStatus deviceStatus=convertGuidanceScreenToDeviceStatus(guidanceScreen);
        deviceStatus.setId(U.id());
        deviceStatus.setCode(MunicipalParkingUtils.generateCode(deviceStatus.getId(),BusinessCodeEnum.DEVICE_STATUS.getCode()));
        parkingLotDeviceStatusMapper.insert(deviceStatus);
        return guidanceScreen;

    }

    private ParkingLotDeviceStatus convertGuidanceScreenToDeviceStatus(ParkingLotDeviceGuidanceScreen guidanceScreen){
        ParkingLotDeviceStatus parkingLotDeviceStatus = new ParkingLotDeviceStatus();
        //todo
//        parkingLotDeviceStatus.setParkingLotSlotId(videoPile.getParkingLotSlotId());
//        parkingLotDeviceStatus.setParkingLotSlotCode(videoPile.getParkingLotSlotCode());
//        parkingLotDeviceStatus.setParkingLotSlotName(videoPile.getParkingLotSlotName());
        parkingLotDeviceStatus.setDeviceId(guidanceScreen.getId());
        parkingLotDeviceStatus.setDeviceCode(guidanceScreen.getCode());
        parkingLotDeviceStatus.setDeviceName(guidanceScreen.getName());
        parkingLotDeviceStatus.setDeviceType(DeviceTypeEnum.GUIDANCE_SCREEN.getCode());
        parkingLotDeviceStatus.setIsOnline(DeviceOperationStatusEnum.OnLine.getCode());
        parkingLotDeviceStatus.setStatus(DeviceStatusEnum.Normal.getCode());
        parkingLotDeviceStatus.setProvinceId(guidanceScreen.getProvinceId());
        parkingLotDeviceStatus.setProvinceName(guidanceScreen.getProvinceName());
        parkingLotDeviceStatus.setCityId(guidanceScreen.getCityId());
        parkingLotDeviceStatus.setCityName(guidanceScreen.getCityName());
        parkingLotDeviceStatus.setDistrictId(guidanceScreen.getDistrictId());
        parkingLotDeviceStatus.setDistrictName(guidanceScreen.getDistrictName());
        parkingLotDeviceStatus.setLocation(guidanceScreen.getLocation());
        parkingLotDeviceStatus.setNote(guidanceScreen.getNote());
//        parkingLotDeviceStatus.setFixBy(null);
//        parkingLotDeviceStatus.setFixTime(null);
//        parkingLotDeviceStatus.setFixNote(null);
        parkingLotDeviceStatus.setLastReportTime(LocalDateTime.now());
        return parkingLotDeviceStatus;
    }

    /**
     * 更新诱导屏
     * @param form
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public ParkingLotDeviceGuidanceScreen updateGuidanceScreen(GuidanceScreenEditForm form) {
        ParkingLotDeviceGuidanceScreen guidanceScreen=BeanUtils.transform(ParkingLotDeviceGuidanceScreen.class, form);
        guidanceScreen.setId(MunicipalParkingUtils.getIdByCode(guidanceScreen.getCode()));
        updateById(guidanceScreen);//更新
        ParkingLotDeviceStatus parkingLotDeviceStatus=convertGuidanceScreenToDeviceStatus(guidanceScreen);
        parkingLotDeviceStatusMapper.update(parkingLotDeviceStatus, new UpdateWrapper<ParkingLotDeviceStatus>().eq("device_id", guidanceScreen.getId()));
        return guidanceScreen;

    }


    /**
     * 获取单个诱导屏实体
     * @param id
     * @return
     */
    public GuidanceScreenDto getGuidanceScreenDto(long id) {
        GuidanceScreenDto dto= parkingLotDeviceGuidanceScreenParkingLotMapper.getGuidanceScreenDto(id);
        if(dto.getParkingLots().size()!=0){
            List<String> ids=new ArrayList<>();
            for(GuidanceScreenParkingLotDTO guidanceScreenParkingLotDTO:dto.getParkingLots()){
                ids.add(guidanceScreenParkingLotDTO.getParkingLotId());
            }
            ApiResult<List<ParkingLotStatusDto>> parkingLotIds=parkingLotFeignClient.getSlotStatByParkingLot(ids);
            Map<String,ParkingLotStatusDto> parkingLotsMap=parkingLotIds.getResult().stream().collect(Collectors.toMap(ParkingLotStatusDto::getParkingLotId, parkingLotStatusDto -> parkingLotStatusDto));
            for(GuidanceScreenParkingLotDTO guidanceScreenParkingLotDTO:dto.getParkingLots()){
                ParkingLotStatusDto parkingLotStatusDto=parkingLotsMap.get(guidanceScreenParkingLotDTO.getParkingLotId());
                guidanceScreenParkingLotDTO.setParkingLotCode(parkingLotStatusDto.getParkingLotCode()).setFreeSlots(String.valueOf(parkingLotStatusDto.getFreeSlots())).setParkingLotName(parkingLotStatusDto.getParkingLotName());
            }
        }

        return dto;
    }

    public void deleteGuidanceScreen(long id) {
        parkingLotDeviceGuidanceScreenMapper.deleteById(id);
        parkingLotDeviceGuidanceScreenParkingLotMapper.deleteByGuidanceScreenId(id);
        parkingLotDeviceStatusMapper.delete(new QueryWrapper<ParkingLotDeviceStatus>().eq("device_id",id));
    }

    public Page<GuidanceScreenDto> getGuidanceScreens(Page<VideoPileDto> page, GuidanceScreenQueryForm form) {

        return parkingLotDeviceGuidanceScreenParkingLotMapper.getGuidanceScreens(page,form);
    }
}
